package com.boot2.core.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author marswon
 * Created on 2016/5/30.
 * ErrorMessage
 * refer: https://www.yunpian.com/api2.0/demo.html
 */
public class YunpianSms {
    public static final String API_KEY = "d2e0f43d1665266b24fba4ff1449090c";

    //智能匹配模板发送接口的http地址
    private final static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";
    
    private final static String URI_ADD_VOICE_NOTIFY = "https://voice.yunpian.com/v2/tpl/add_voice_notify.json";
    private final static String URI_VOICE_NOTIFY = "https://voice.yunpian.com/v2/voice/tpl_notify.json";
    private final static String VOICE_NOTIFY_SMOKE_WARNING_TPL_ID = "1850174";  //烟雾报警模板
    
    //编码格式。发送编码格式统一用UTF-8
    private final static String ENCODING = "UTF-8";

    public static void singleSend(String phone_number,String content) {
        String r1 = null;
        try {
            r1 = sendSms(API_KEY,content,phone_number);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static String sendSms(String apikey, String text, String mobile) throws IOException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("text", text);
        params.put("mobile", mobile);
        return post(URI_SEND_SMS, params);
    }
    
//  public static void addVoiceNotify() {
//  Map<String, String> params = new HashMap<>();
//  params.put("apikey", API_KEY);
//  params.put("tpl_content", "您的#device#发现烟雾");
//  String result = post(URI_ADD_VOICE_NOTIFY, params);
//  System.out.println(result);
//}
    
    private static String sendVoiceNotify(String mobile, String tpl_id, String tpl_value, String callback_url){
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", API_KEY);
        params.put("mobile", mobile);
        params.put("tpl_id", tpl_id);
        params.put("tpl_value", tpl_value);
        if(callback_url != null) {
            params.put("callback_url", callback_url);  //语音状态报告推送地址
        }
        return post(URI_VOICE_NOTIFY, params);
    }
    
    /**
     * 发送烟雾报警语音通知
     * @param mobile 用户手机号
     * @param device 烟雾报警器设备名
     * @return
     * @throws IOException
     */
    public static String sendSmokeWarningVoiceNotify(String mobile, String device) throws IOException {
        String callback_url = "http://develop.chltec.com/yunpian/callback";
        return sendVoiceNotify(mobile,VOICE_NOTIFY_SMOKE_WARNING_TPL_ID,URLEncoder.encode("device="+device,"utf-8"),callback_url);
    }

    /**
     * 基于HttpClient 4.3的通用POST方法
     *
     * @param url       提交的URL
     * @param paramsMap 提交<参数，值>Map
     * @return 提交响应
     */

    public static String post(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return responseText;
    }
}
